const path = require("path");
const managersDAO = require(path.join(process.cwd(), "/dao/ManagerDAO"));
const Password = require("node-php-password");
/**
 * 获取用户列表
 * @param {*} conditions 条件
 * @param {*} cb
 */
module.exports.getManagers = function (conditions, cb) {
  const { query: key, pagenum, pagesize } = conditions;
  if (!pagenum) return cb("pagenum 参数不合法");
  if (!pagesize) return cb("pagenum 参数不合法");

  managersDAO.countByKey(key, function (err, count) {
    const pageCount = Math.ceil(count / pagesize);
    let offset = (pagenum - 1) * pagesize;
    if (offset >= count) {
      offset = count;
    }
    managersDAO.findByKey(
      key,
      offset,
      Number(pagesize),
      function (err, managers) {
        if (err) return cb(err);
        let managersList = [];
        for (let i in managers) {
          const manager = managers[i];
          managersList.push({
            id: manager.mg_id,
            role_name: !manager.role_id ? "超级管理员" : manager.role_name,
            username: manager.mg_name,
            create_time: manager.mg_time,
            mobile: manager.mg_mobile,
            email: manager.mg_email,
            mg_state: manager.mg_state == 1,
          });
        }
        cb(err, {
          total: count,
          pagenum: pageCount,
          users: managersList,
        });
      }
    );
  });
};

/**
 * 新增用户
 * @param {*} params
 * @param {*} cb
 */
module.exports.createManagers = function (params, cb) {
  const { username, password, mobile, email, rid } = params;
  managersDAO.exists(username, function (err, isExists) {
    if (err) return cb(err);
    if (isExists) {
      return cb("用户名已存在");
    }
    managersDAO.create(
      {
        mg_name: username,
        mg_pwd: Password.hash(password),
        mg_mobile: mobile,
        mg_email: email,
        mg_time: Date.parse(new Date()) / 1000,
        role_id: rid,
      },
      function (err, manager) {
        if (err) return res.sendResult(500, err, "");
        cb({
          id: manager.mg_id,
          username: manager.mg_name,
          mobile: manager.mg_mobile,
          email: manager.mg_email,
          role_id: manager.role_id,
          create_time: manager.mg_time,
        });
      }
    );
  });
};

/**
 * 修改
 * @param {*} params
 * @param {*} cb
 */
module.exports.modifyManagers = function (params, cb) {
  managersDAO.update(
    {
      mg_id: params.id,
      mg_mobile: params.mobile,
      mg_email: params.email,
    },
    function (err, person) {
      if (err) return cb("更新失败" + err);
      cb(null, {
        id: person.mg_id,
        username: person.mg_name,
        role_id: person.role_id,
        mobile: person.mg_mobile,
        email: person.mg_email,
      });
    }
  );
};

module.exports.deleteManager = function (id, cb) {
  managersDAO.delete(id, function (err, person) {
    if (err) return cb("删除失败" + err);
    cb(null, {
      id: person.mg_id,
      username: person.mg_name,
      role_id: person.role_id,
      mobile: person.mg_mobile,
      email: person.mg_email,
    });
  });
};

/**
 * 登录
 * @param {*} username
 * @param {*} password
 * @param {*} cb
 */
module.exports.login = function (username, password, cb) {
  managersDAO.findOne({ mg_name: username }, function (err, person) {
    if (err) return cb("验证失败" + err);
    const isVerify = Password.verify(password, person.mg_pwd);
    if (!isVerify) {
      return cb("密码错误");
    }
    cb(null, {
      id: person.mg_id,
      username: person.mg_name,
      role_id: person.role_id,
      mobile: person.mg_mobile,
      email: person.mg_email,
    });
  });
};
